# Maintainer: George Rawlinson <george@rawlinson.net.nz>
# Contributor: graysky <graysky AT archlinux DOT us>
# Contributor: Stéphane Graber <stgraber AT ubuntu DOT com>

pkgname=distrobuilder
pkgver=3.0
pkgrel=1
pkgdesc='System container image builder for Incus and LXC'
arch=('loong64' 'x86_64')
url='https://linuxcontainers.org/distrobuilder/'
license=('Apache')
depends=(
  'rsync'
  'squashfs-tools'
  'gnupg'
  'debootstrap'
  'dosfstools'
  'gptfdisk'
  'qemu-img'
  'e2fsprogs'
  'btrfs-progs'
)
makedepends=('go' 'git')
optdepends=(
  'cdrtools: for repack-windows command'
  'hivex: for repack-windows command'
  'wimlib: for repack-windows command'
)
options=('!lto')
source=("https://linuxcontainers.org/downloads/$pkgname/$pkgname-$pkgver.tar.gz"{,.asc})
sha512sums=('c2218e6312257e5f15077d8380b9b49f7b8afd7ed5e43d46ac5d18a8cc332b096ab50c7301f74eb93769548c10f273e21a584ab2e908260ead4d2b219683bd1c'
            'SKIP')
b2sums=('71cf594cdece46890418ea58af06205c59beaecce46a977efadcb336b93f0e1ed85ba4c1563a1f9f95937e935f844c91b6e45dfc73c0210f134ba3a694adea1d'
        'SKIP')
validpgpkeys=('602F567663E593BCBD14F338C638974D64792D67') # Stéphane Graber <stgraber@stgraber.org>

prepare() {
  cd "$pkgname-$pkgver"

  # create folder for build output
  mkdir build
}

build() {
  cd "$pkgname-$pkgver"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  # verify modules
  go mod verify

  go build -v \
    -buildmode=pie \
    -mod=vendor \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS}" \
    -o build \
    ./...
}

# failing due to /tmp issues
#check() {
#  cd "$pkgname-$pkgver"
#  go test -v ./...
#  go vet -v ./...
#}

package() {
  install -vDm755 -t "$pkgdir/usr/bin" "$pkgname-$pkgver/build/$pkgname"
}
